﻿package com.sh.game.util
{
    import flash.utils.*;

    public class SecondTimerUtil extends Object
    {
        private var _tasks:Dictionary;
        private var _lastTime:Number;
        private var __id:int = 0;
        private static var _instance:SecondTimerUtil = null;

        public function SecondTimerUtil()
        {
            this._tasks = new Dictionary();
            return;
        }// end function

        public function addSecondTask(param1:Function) : int
        {
            var _loc_2:String = this;
            var _loc_3:* = this.__id + 1;
            _loc_2.__id = _loc_3;
            this._tasks[this.__id] = param1;
            return this.__id;
        }// end function

        public function removeTask(param1:int) : void
        {
            if (param1 in this._tasks)
            {
                this._tasks[param1] = null;
                delete this._tasks[param1];
            }
            return;
        }// end function

        public function length() : int
        {
            var _loc_2:Object = null;
            var _loc_1:int = 0;
            for each (_loc_2 in this._tasks)
            {
                
                _loc_1++;
            }
            return _loc_1;
        }// end function

        public function update() : void
        {
            var _loc_4:Object = null;
            var _loc_1:* = new Date().getTime();
            if (!this._lastTime)
            {
                this._lastTime = _loc_1;
                return;
            }
            var _loc_2:* = _loc_1 - this._lastTime;
            var _loc_3:int = 10;
            while (_loc_2 > 1000 && _loc_3 > 0)
            {
                
                for each (_loc_4 in this._tasks)
                {
                    
                    if (_loc_4 != null)
                    {
                        this._loc_4();
                    }
                }
                _loc_2 = _loc_2 - 1000;
                _loc_3 = _loc_3 - 1;
            }
            this._lastTime = _loc_1 - _loc_2;
            return;
        }// end function

        public static function get instance() : SecondTimerUtil
        {
            if (_instance == null)
            {
                _instance = new SecondTimerUtil;
            }
            return _instance;
        }// end function

    }
}
